from dataset_builder import DatasetBuilder
from agent_comparator import AgentComparator
from agents import agents10,agents25,agents50,agents75,agents100,AgentStirlitz
from time import strftime
from our_agent_comparator import AgentClassify

dimaDP = 'E:/Documents/Dropbox/Dropbox/AI_hw3/dima/'
alexDP = 'C:/Users/Alex/Dropbox/AI_hw3/'
path = alexDP

def createRealDatasets(dir = 'topics/'):
    '''
    @return: A dictionary of the real datasets, with their names as keys.
    '''
    builder = DatasetBuilder()
    datasets = {}
    datasets['Windows 7: Features or speed?'] = builder.fromFiles([dir+'features_windows7.txt.data', dir+'speed_windows7.txt.data'])
    datasets['Best Western Hotels: Bath or room?'] = builder.fromFiles([dir+'bathroom_bestwestern_hotel_sfo.txt.data', dir+'rooms_bestwestern_hotel_sfo.txt.data'])
    datasets['Hotels: San Francisco or Chicago?'] = builder.fromFiles([dir+'rooms_bestwestern_hotel_sfo.txt.data', dir+'rooms_swissotel_chicago.txt.data'])
    datasets['Battery life: Which device?'] = builder.fromFiles([dir+'battery-life_amazon_kindle.txt.data', dir+'battery-life_ipod_nano_8gb.txt.data', dir+'battery-life_netbook_1005ha.txt.data'])
    datasets['iPod Nano: Which aspect?'] = builder.fromFiles([dir+'battery-life_ipod_nano_8gb.txt.data', dir+'screen_ipod_nano_8gb.txt.data', dir+'sound_ipod_nano_8gb.txt.data', dir+'video_ipod_nano_8gb.txt.data'])
    return datasets

def runExperiment(name, data, agent1_class, agent2_class, classification_time_limit, learning_time_limit, filename):
    '''
    Runs the comparator with the given parameters and prints its output.
    '''
    
    f = open(path+filename,"a")
    confusion1, confusion2, mcnemar = AgentClassify().run(data, agent1_class, agent2_class, classification_time_limit, learning_time_limit, 10)
#    confusion1, confusion2, mcnemar = AgentComparator().run(data, agent1_class, agent2_class, classification_time_limit, learning_time_limit, 10)    
#    f.write(name+'\n')
#    f.write('======================================================='+'\n')
    f.write(str(confusion1.getAccuracy()*100)+'\n')
#    f.write('======================================================='+'\n')
#    f.write(str(confusion2)+'\n')
#    f.write('======================================================='+'\n')
#    f.write(str(mcnemar)+'\n')
#    f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'+'\n')
#    f.write('\n')
    f.close()

bagSize = [30,70]
# Real Data #

def clash(agents,bs):
    datasets = createRealDatasets()
#    print end
    for size in bagSize:
        agents = []
        for stem in (False, True):
            for elim in (False, True):
                agents.append(AgentStirlitz(size, stem, elim))
        file_name = str(size) + 'words'
        f = open(path+file_name+'.out',"a")
        f.write(file_name+'\n')
        f.close()     
        for name, dataset in datasets.items():
            #name_file = name.rsplit(":")[0]   
            for bob in agents:
    #            for j in xrange(i+1,end):
                f = open(path+file_name+'.out',"a")
                f.write(name + '\t' + str(bob)+'\t')
                f.close()
                runExperiment(name, dataset, bob, None, 1, 30, file_name+'.out')
                print str(str(str(bob).rsplit(',')[0]).rsplit(' ')[0]),'Done',strftime(' %H:%M:%S '),name
#runs:
basicAgents = []
#i=5
#while i < 101:
#    basicAgents.append(AgentStirlitz(i, False, False))
#    i += 5
clash(basicAgents,'')
#clash(agents10,'10')
#clash(agents25,'25')
#clash(agents50,'50')
#clash(agents75,'75')
#clash(agents100,'100')